package moonstruckdrops.page;

import moonstruckdrops.dao.EntryDao;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.model.Model;

import com.google.appengine.api.datastore.Text;

public class ModifiedEntryPage extends WebPage{
	
	String inputTitleText="";
	String inputContentText="";
	
	public ModifiedEntryPage(final Long id,String EntryTitle,String Entry){
		final TextField<String> Title =new TextField<String>("EntryTitle",new Model<String>(EntryTitle));//PropertyModel<String>(this,"inputTitleText"));
		final TextArea<String> Content = new TextArea<String>("EntryContent",new Model<String>(Entry));//PropertyModel<String>(this,"inputContentText"));
		Form<Void> EntryForm =ModifiedForm("EntryForm",Title,Content,id);
		add(EntryForm);
		EntryForm.add(Title);
		EntryForm.add(Content);	
	}
	
	private Form<Void> ModifiedForm(String WicketID,final TextField<String> Title,final TextArea<String> Content,final Long id){
			return new Form<Void>(WicketID){
				private static final long serialVersionUID = 1L;
				public void onSubmit(){
					String updateTitle=UpdateTitle(Title);
					Text updateContent=UpdateEntryContent(Content);
					UpdateEntry(id, updateTitle, updateContent);
					setResponsePage(BlogMainPage.class);
			}
				
		};
	}
	
	private String UpdateTitle(TextField<String> Title){
		return Title.getInput();
	}
	
	private Text UpdateEntryContent(TextArea<String> Content){
		return new Text(Content.getInput());
	}
	
	private void UpdateEntry(Long id,String updateTitle,Text updateContent){
		EntryDao dao = new EntryDao();
		dao.UpDateEntry(id,updateTitle,updateContent);
	}
}
